/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generation.objectinfo;

import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.runtime.Response;
import com.farmcode.api.runtime.RuntimeServerAPI;
import com.farmcode.api.runtime.RuntimeServerException;
import com.farmcode.api.runtime.metadata.ChildCollection;
import com.farmcode.api.runtime.metadata.ObjectInfo;
import com.farmcode.web.generation.BaseCodeGenerator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
//import org.primefaces.model.TreeNode;

/**
 * 
 * @author oscar
 */
public abstract class BaseObjectInfo extends BaseCodeGenerator {
    
    /**
     * 
     */
    public static final String CURRENT_OBJECT_INFO = ObjectInfo.class.getName()+ ".CURRENT_OBJECT_INFO";
    /**
     * 
     */
    public static final String CURRENT_ATTIRBUTES_SELECTED = ObjectInfo.class.getName() + ".ATTRIBUTES";

    @EJB
    private RuntimeServerAPI runtimeServer;
    private List<ObjectInfo> objectInfos;
    private ObjectInfo currentObjectInfo;
    private ChildCollection detail;
    
    private boolean selectAll;
    

//    private TreeNode root;

    /**
     * 
     */
    @PostConstruct
    public void init() {
    }

    /**
     * 
     * @return
     */
    public abstract Object getObjectType();
    
    /**
     * 
     * @return
     */
    public abstract String getSelectedAllSessionName();
    
    /**
     * 
     */
    public abstract boolean prepare();

    /**
     * 
     */
    public void doSelectAll(){
         List<ObjectInfo> infos = getSession().getValue(getObjectInfoSessionName());
         if(infos != null){
             for (ObjectInfo objectInfo : infos) {
                 objectInfo.setUsed(isSelectAll());
             }
             getSession().setValue(getObjectInfoSessionName(), infos);
         }
    }
    
    /**
     * 
     * @return
     */
    public abstract String getObjectInfoSessionName();

    /**
     * 
     * @param objectInfo
     * @return
     */
    public String objectSelected(ObjectInfo objectInfo) {
        try {
            String message = java.util.ResourceBundle.getBundle("com/farmcode/i18n/message").getString("generator.objectinfo.selectedinfo");
            super.showMessage(String.format(message, objectInfo.getName()));
            
            List<ObjectInfo> infos = getSession().getValue(getObjectInfoSessionName());
            
            for (ObjectInfo info : infos) {
                if (info.getName().equals(objectInfo.getName())) {
                     setCurrentObjectInfo(info);                     
                     FARMCodeContext context = getContext();
                     prepare();
                     context.put(FARMCodeContext.CLASS_TYPE, info);
                     Response<ObjectInfo> response = runtimeServer.getColumn(context);
                     info = response.getResult();
                     setDetail(info.getChilds());
                    break;
                }
            }
            getSession().setValue(getObjectInfoSessionName(), infos);
        } catch (Exception ex) {
           Logger.getLogger(BaseObjectInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * 
     * @param farmcodeContext
     * @throws RuntimeServerException
     */
    public void findObjectInfo(FARMCodeContext farmcodeContext) throws RuntimeServerException {
        Response response = runtimeServer.getMetadata(getContext());
        setObjectInfos((List<ObjectInfo>) response.getResult());
    }

    /**
     * @return the objectInfos<ui:include src="/WEB-INF/facelets/generation/variables.xhtml"/>
     */
    public List<ObjectInfo> getObjectInfos() {
        
       if (getSession().getValue(getObjectInfoSessionName()) == null) {
            try {
                if(prepare()){
                    Response<List<ObjectInfo>> metadata = runtimeServer.getMetadata(getContext());
                    setObjectInfos(metadata.getResult());
                }
            } catch (RuntimeServerException ex) {
                Logger.getLogger(BaseObjectInfo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        objectInfos = getSession().getValue(getObjectInfoSessionName());
        return objectInfos;
    }

    /**
     * @param objectInfos the objectInfos to set
     */
    public void setObjectInfos(List<ObjectInfo> objectInfos) {
        this.objectInfos = objectInfos;
        getSession().setValue(getObjectInfoSessionName(), objectInfos);
    }

    /**
     * @return the currentObjectInfo
     */
    public ObjectInfo getCurrentObjectInfo() {

        if(getSession().getValue(CURRENT_OBJECT_INFO) != null){
            currentObjectInfo = getSession().getValue(CURRENT_OBJECT_INFO);
        }
        return currentObjectInfo;
    }

    /**
     * @param currentObjectInfo the currentObjectInfo to set
     */
    public void setCurrentObjectInfo(ObjectInfo currentObjectInfo) {
        this.currentObjectInfo = currentObjectInfo;
        getSession().setValue(CURRENT_OBJECT_INFO, currentObjectInfo);
    }

//    /**
//     * @return the root
//     */
//    public TreeNode getRoot() {
//        return root;
//    }
//
//    /**
//     * @param root the root to set
//     */
//    public void setRoot(TreeNode root) {
//        this.root = root;
//    }

    /**
     * @return the detail
     */
    public ChildCollection getDetail() {
        if(getSession().getValue(CURRENT_ATTIRBUTES_SELECTED) != null){
            detail = getSession().getValue(CURRENT_ATTIRBUTES_SELECTED);
        }
        return detail;
    }

    /**
     * @param detail the detail to set
     */
    public void setDetail(ChildCollection detail) {
        this.detail = detail;
        getSession().setValue(CURRENT_ATTIRBUTES_SELECTED, detail);
    }

    /**
     * @return the selectAll
     */
    public boolean isSelectAll() {
        if(  getSession().getValue(getSelectedAllSessionName()) != null){
              selectAll = Boolean.parseBoolean(getSession().getValue(getSelectedAllSessionName()).toString());
        }
        return selectAll;
    }

    /**
     * @param selectAll the selectAll to set
     */
    public void setSelectAll(boolean selectAll) {
        this.selectAll = selectAll;
        getSession().setValue(getSelectedAllSessionName(), selectAll);
    }

}
